| Conditions | 7 |
| Total Lines | 96 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import React, { useState, useEffect } from 'react'; |
||
| 7 | |||
| 8 | export default function QrScanner({navigation, cameraVisible, setCameraVisible, scooter, setModalVisible, currentCity, setCurrentScooter}) { |
||
| 9 | const [hasPermission, setHasPermission] = useState(null); |
||
| 10 | const [scanned, setScanned] = useState(false); |
||
| 11 | const [code, setCode] = useState(null); |
||
| 12 | |||
| 13 | useEffect(() => { |
||
| 14 | const getBarCodeScannerPermissions = async () => { |
||
| 15 | const { status } = await BarCodeScanner.requestPermissionsAsync(); |
||
| 16 | setHasPermission(status === 'granted'); |
||
| 17 | }; |
||
| 18 | |||
| 19 | getBarCodeScannerPermissions(); |
||
| 20 | }, []); |
||
| 21 | |||
| 22 | async function handleBarCodeScanned({ type, data }) { |
||
| 23 | setScanned(true); |
||
| 24 | |||
| 25 | await startScooter(data); |
||
| 26 | }; |
||
| 27 | |||
| 28 | if (hasPermission === null) { |
||
| 29 | return <Text>Requesting for camera permission</Text>; |
||
| 30 | } |
||
| 31 | if (hasPermission === false) { |
||
| 32 | return <Text>No access to camera</Text>; |
||
| 33 | } |
||
| 34 | |||
| 35 | async function startScooter(scooter): Promise<void> { |
||
| 36 | // Check if QR code is a valid scooter |
||
| 37 | const result = await scooterModel.checkIfValidScooter(scooter, currentCity); |
||
| 38 | |||
| 39 | |||
| 40 | if (result) { |
||
| 41 | showMessage({ |
||
| 42 | message: 'Scooter scanned!', |
||
| 43 | type: 'success', |
||
| 44 | position: 'bottom' |
||
| 45 | |||
| 46 | }); |
||
| 47 | |||
| 48 | setCameraVisible(!cameraVisible); |
||
| 49 | setModalVisible(true); |
||
| 50 | setCurrentScooter(result); |
||
| 51 | } else { |
||
| 52 | showMessage({ |
||
| 53 | message: 'Not a scooter!', |
||
| 54 | type: 'danger', |
||
| 55 | position: 'center' |
||
| 56 | }) |
||
| 57 | } |
||
| 58 | |||
| 59 | |||
| 60 | } |
||
| 61 | |||
| 62 | return ( |
||
| 63 | <Modal |
||
| 64 | animationType="slide" |
||
| 65 | transparent={true} |
||
| 66 | visible={cameraVisible} |
||
| 67 | onRequestClose={() => { |
||
| 68 | setCameraVisible(!cameraVisible); |
||
| 69 | }} |
||
| 70 | > |
||
| 71 | <View style={[styles.container, styles.shadowProp]}> |
||
| 72 | <Pressable style={[styles.backButton, styles.shadowProp]} onPress={() => setCameraVisible(!cameraVisible)}> |
||
| 73 | <Icon |
||
| 74 | name='x' |
||
| 75 | size={25} |
||
| 76 | color='black' |
||
| 77 | /> |
||
| 78 | </Pressable> |
||
| 79 | |||
| 80 | <Text style={styles.title}>Scan the QR-code</Text> |
||
| 81 | <TextInput |
||
| 82 | placeholder="or enter code manually" |
||
| 83 | style={styles.input} |
||
| 84 | onChangeText={(content: string) => { |
||
| 85 | setCode(content) |
||
| 86 | }} |
||
| 87 | onSubmitEditing={() => startScooter(code)} |
||
| 88 | /> |
||
| 89 | <BarCodeScanner |
||
| 90 | onBarCodeScanned={scanned ? undefined : handleBarCodeScanned} |
||
| 91 | style={styles.viewFinder} |
||
| 92 | /> |
||
| 93 | |||
| 94 | <Pressable style={[styles.cameraButton, styles.shadowProp]} onPress={() => setScanned(false)} > |
||
| 95 | <Icon |
||
| 96 | name='screen-full' |
||
| 97 | size={24} |
||
| 98 | color='black' |
||
| 99 | /> |
||
| 100 | </Pressable> |
||
| 101 | </View> |
||
| 102 | </Modal> |
||
| 103 | |||
| 181 | }) |